﻿Public Class XmlResult
    Inherits ActionResult
    Private m_objectToSerialize As Object

    ''' <summary> 
    ''' Initializes a new instance of the <see cref="XmlResult"/> class. 
    ''' </summary> 
    ''' <param name="objectToSerialize">The object to serialize to XML.</param> 
    Public Sub New(objectToSerialize As Object)
        Me.m_objectToSerialize = objectToSerialize
    End Sub

    ''' <summary> 
    ''' Gets the object to be serialized to XML. 
    ''' </summary> 
    Public ReadOnly Property ObjectToSerialize() As Object
        Get
            Return Me.m_objectToSerialize
        End Get
    End Property

    ''' <summary> 
    ''' Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream. 
    ''' </summary> 
    ''' <param name="context">The controller context for the current request.</param> 
    Public Overrides Sub ExecuteResult(context As ControllerContext)
        If Me.m_objectToSerialize IsNot Nothing Then
            context.HttpContext.Response.Clear()
            Dim xs = New System.Xml.Serialization.XmlSerializer(Me.m_objectToSerialize.[GetType]())
            context.HttpContext.Response.ContentType = "text/xml"
            xs.Serialize(context.HttpContext.Response.Output, Me.m_objectToSerialize)
        End If
    End Sub
End Class